package cn.skylark.wcm.common.splitter.doc;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 拆分后的数组
 */
public class SplitterArray {
    public static List<Integer> docSplitterArry(String fileName) throws Throwable {
        List<Integer> arraySplitterList = new ArrayList<>();
        File file = new File(fileName);
        //判断是一个目录的话
        if (file.isDirectory()) {
            if (file.isDirectory()) {
                for (File eachfile : file.listFiles()) {
                    if (eachfile.isDirectory()) {
                        //然后对每一层文件进行便历，如果是目录的话
                        File dir = new File(eachfile.getName() + "\\");
                        dir.mkdir();

                    } else {
                        //如果不是目录的话，判断是以doc还是docx结尾
                        if (eachfile.getName().endsWith(".doc") || eachfile.getName().endsWith(".DOC")) {


                        } else if (eachfile.getName().endsWith(".docx") || eachfile.getName().endsWith(".DOCX")) {
                            //docx的拆分
                            // DocxSplitter.docxsplithtmltext(fileName,String.valueOf(level));
                        }

                    }
                }
            }
        } else {
            //如果是一个文件的话 doc 格式
            if (file.getName().endsWith(".doc") || file.getName().endsWith(".DOC")) {
                //1.如果是doc文档
                InputStream is = new FileInputStream(fileName);
                HWPFDocument doc = new HWPFDocument(is);
                Range range = doc.getRange();
                int paraNum = range.numParagraphs();//段落数
                System.out.println(paraNum);
                Paragraph para;
                List<Integer> titleList0 = new ArrayList<>();
                List<Integer> titleList1 = new ArrayList<>();
                List<Integer> titleList2 = new ArrayList<>();
                List<Integer> titleList3 = new ArrayList<>();
                //获取到所有的级别
                for (int i = 0; i < paraNum; i++) {
                    para = range.getParagraph(i);
                    int Lvl = para.getLvl();//获取级别
                    titleList0.add(Lvl);
                }
                //进行判断是否有大纲，没大钢的话按字体大小来排
                if (titleList0.contains(0) || titleList0.contains(1) || titleList0.contains(2) || titleList0.contains(3)||titleList0.contains(4)||titleList0.contains(5)||titleList0.contains(6)) {
                    for (int i = 0; i < paraNum; i++) {
                        //说明有大纲，然后就根据大纲来解析
                        para = range.getParagraph(i);
                        int Lvl = para.getLvl();//获取级别
                        if (Lvl == 0) {
                            arraySplitterList.add(i);
                        } else if (Lvl == 1) {
                            arraySplitterList.add(i);
                        } else if (Lvl == 2) {
                            arraySplitterList.add(i);
                        }else if (Lvl == 3){
                            arraySplitterList.add(i);
                        }else if (Lvl == 4){
                            arraySplitterList.add(i);
                        }
                    }
                    arraySplitterList.add(paraNum);
//                    for (int i = 0; i < arraySplitterList.size(); i++) {
//                        System.out.println("2132===="+arraySplitterList.get(i));
//                    }
                } else {
                    List<Integer> list = new ArrayList<>();
                    List<String> list2 = new ArrayList<>();
                    for (int i = 0; i < paraNum; i++) {
                        para = range.getParagraph(i);
                        for (int z = 0; z < para.numCharacterRuns(); z++) {
                            CharacterRun run = para.getCharacterRun(z);
                            list.add(run.getFontSize());
                            run.getFontSize(); //字体大小
                            //把字体的大小连带段落数放到一个map集合中
                            //System.out.println(run.text() + ">" + run.getFontSize() + "/" + i);
                            list2.add(run.text() + ">" + run.getFontSize() + "/" + i);
                            run.getColor();
                        }
                    }
//                    for (int i=0;i<list2.size();i++){
//                        System.out.println("2332gg=="+list2.get(i));
//                    }
                    //第一次去重
                    Integer[] integers = list.toArray(new Integer[0]);
                    List<String> list1 = new ArrayList<>();
                    for (int i = 0; i < integers.length; i++) {
                        for (int j = i + 1; j < integers.length; j++) {
                            if (integers[i] == integers[j]) {
                                j = ++i;
                            }
                        }
                        list1.add(integers[i].toString());
                    }
                    String[] arrayResult = (String[]) list1.toArray(new String[list1.size()]);
                    Arrays.sort(arrayResult);  //进行排序
                    for (int i = 0; i < arrayResult.length; i++) {
                        System.out.println("dad::" + arrayResult[i]);
                    }
                    //得到第二大的字号
//                    System.out.println("sdadasds:" + arrayResult[arrayResult.length-2]);
                    Integer font2 = Integer.valueOf(arrayResult[arrayResult.length - 2]);
                    if (arrayResult.length <= 2) {
                        int font1 = Integer.valueOf(arrayResult[arrayResult.length - 1]);
                        List<Integer> splitterPara0 = new ArrayList<>();
                        for (int y = 0; y < list2.size(); y++) {
                            String content = list2.get(y);
                            String text = content.substring(0, content.lastIndexOf(">"));
                            String fontSize = content.substring(content.lastIndexOf(">") + 1, content.indexOf("/"));
                            String index = content.substring(content.lastIndexOf("/") + 1);
                            if ((font1 == Integer.valueOf(fontSize)) && (text != null && !" ".equals(text))) {
                                splitterPara0.add(Integer.valueOf(index));
                            }

                        }
                        //然后进行去重
                        //得到的最终数组
                        Integer[] integers0 = splitterPara0.toArray(new Integer[0]);
                        //去重的方法
                        for (int i = 0; i < integers0.length; i++) {
                            for (int j = i + 1; j < integers0.length; j++) {
                                if (integers0[i] == integers0[j]) {
                                    j = ++i;
                                }
                            }
                            arraySplitterList.add(integers0[i]);
                        }
                        arraySplitterList.add(paraNum);
                    } else {
                        List<Integer> splitterPara = new ArrayList<>();
                        //然后对list2进行遍历
                        for (int y = 0; y < list2.size(); y++) {
                            String content = list2.get(y);
                            String text = content.substring(0, content.lastIndexOf(">"));
                            String fontSize = content.substring(content.lastIndexOf(">") + 1, content.indexOf("/"));
                            String index = content.substring(content.lastIndexOf("/") + 1);
                            if ((font2 == Integer.valueOf(fontSize)) && (text != null && !"".equals(text))) {
                                splitterPara.add(Integer.valueOf(index));
                            }

                        }
                        //得到的最终数组
                        Integer[] integers1 = splitterPara.toArray(new Integer[0]);
                        //去重的方法
                        for (int i = 0; i < integers1.length; i++) {
                            for (int j = i + 1; j < integers1.length; j++) {
                                if (integers1[i] == integers1[j]) {
                                    j = ++i;
                                }
                            }
                            arraySplitterList.add(integers1[i]);
                        }
                        arraySplitterList.add(paraNum);
                    }
                }
            }
        }
        return arraySplitterList;
    }
}